<?php
session_start();
if (isset($_SESSION["username"])) {
?>

<?php
header("Content-type:text/html;charset=UTF-8");
include_once("../../untils/conn.php");
mysqli_query($con, "set names utf8");
                if ($con) {
                    if ($db) {
                        //获取数据总行数
                        $sortsql = "select count(*) from oder";
                        $sortdata = mysqli_query($con, $sortsql);
                        $sortresult = mysqli_fetch_row($sortdata);
                        $listsql = "select count(*) from list";
                        $listdata = mysqli_query($con, $listsql);
                        $listresult = mysqli_fetch_row($listdata);
                        $sql = "select * from list  limit 0,8";
                        $data = mysqli_query($con, $sql);
                        $channelsql = "select count(*) from channel";
                        $channeltdata = mysqli_query($con, $channelsql);
                        $channelresult = mysqli_fetch_row($channeltdata);
                        $totalPrice = "SELECT SUM(price) AS total FROM oder WHERE card_sta = 1";
                        $totalData = mysqli_query($con, $totalPrice);
                        $totalResult = mysqli_fetch_assoc($totalData);
                        $sum = $totalResult['total'];
                        
                        //查询代理总数
                        $proxySql = "SELECT COUNT(*) FROM proxy";
                        $proxyData = mysqli_query($con, $proxySql);
                        $proxyResult = mysqli_fetch_row($proxyData);
                        $proxyCount = $proxyResult[0];
                        
                        // 获取当前月份的时间戳范围
                        $currentMonthStart = strtotime(date('Y-m-01 00:00:00'));
                        $currentMonthEnd = strtotime(date('Y-m-t 23:59:59'));
                        
                        // 查询当前月份新增代理数量
                        $newProxySql = "SELECT COUNT(*) FROM proxy WHERE cr_time BETWEEN '$currentMonthStart' AND '$currentMonthEnd'";
                        $newProxyData = mysqli_query($con, $newProxySql);
                        $newProxyResult = mysqli_fetch_row($newProxyData);
                        $newProxyCount = $newProxyResult[0];

                        //查询所有uid即作为用户
                        $sql = "SELECT COUNT(DISTINCT uid) AS unique_uid_count FROM oder";
                        $data = mysqli_query($con, $sql);
                        $result = mysqli_fetch_assoc($data);
                        $uniqueUidCount = $result['unique_uid_count'];
                        
                        // 计算新增的用户
                        // 获取当前月份的时间戳范围
                        $startTime = strtotime(date('Y-m-01'));
                        $endTime = strtotime(date('Y-m-01', strtotime('+1 month')));
                        
                        // 查询本月新增的数据数量
                        $sql = "SELECT COUNT(DISTINCT uid) AS unique_uid_count FROM oder WHERE time >= $startTime AND time < $endTime";
                        $data = mysqli_query($con, $sql);
                        $result = mysqli_fetch_assoc($data);
                        $uniqueUidCountThisMonth = $result['unique_uid_count'];
                        
                        $type = "web_config";
                        $menu_name = '运营平台'; // 设置默认菜单名
                        $sql = "SELECT * FROM foundation WHERE type='$type'";
                        $data = mysqli_query($con, $sql);
                        $result = mysqli_fetch_assoc($data);
                        $site_name = $result['site_name'];

                    }
                }
                
                
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title><?php echo $site_name ?> - 可视化大数据监控大屏</title>
    <link rel="stylesheet" href="./css/index.css">
    <link rel="stylesheet" href="./fonts/icomoon.css">
</head>

<body>
    <div class="viewport">
        <div class="column">
            <!--概览-->
            <div class="overview panel">
                <div class="inner">
                    <div class="item">
                        <h4><?php echo $sum ?></h4>
                        <span>
                            <i class="icon-dot" style="color: #006cff"></i>
                            平台总金额
                        </span>
                    </div>
                    <div class="item">
                        <h4><?php echo $sortresult[0] ?></h4>
                        <span>
                            <i class="icon-dot" style="color: #6acca3"></i>
                            订单总数
                        </span>
                    </div>
                    <div class="item">
                        <h4><?php echo $listresult[0] ?></h4>
                        <span>
                            <i class="icon-dot" style="color: #6acca3"></i>
                            产品总数
                        </span>
                    </div>
                    <div class="item">
                        <h4><?php echo $proxyCount ?></h4>
                        <span>
                            <i class="icon-dot" style="color: #ed3f35"></i>
                            代理总数
                        </span>
                    </div>
                </div>
            </div>
            
            <!--监控-->
             <?php
            include_once("../../untils/conn.php");
            mysqli_query($con, "set names utf8");
            $sql = "SELECT time, sf, city, xian, dizhi, beizhu FROM oder WHERE status=4";
            
            $result = $con->query($sql); // 执行查询
            
            $rows = array(); // 存储每行数据的数组
            
            if ($result->num_rows > 0) {
                // 将每行数据存储到数组中
                while($row = $result->fetch_assoc()) {
                    $rows[] = $row;
                }
            } else {
                // echo "未找到匹配数据";
            }  
            ?>
            
            <div class="monitor panel">
                <div class="inner">
                    <div class="tabs">
                        <a href="javascript:;" data-index="0" class="active">开卡异常用户监控</a>
                        <a>数据将在 <span id="countdown">10</span> 秒后自动刷新</a>
                    </div>
                    <div class="content" style="display: block;">
                        <div class="head">
                            <span class="col">异常时间</span>
                            <span class="col">用户地址</span>
                            <span class="col">异常原因</span>
                        </div>
                        <div class="marquee-view">
                            <div class="marquee">
                                <?php
                                // 使用循环轮流输出每行数据
                                for ($i=0; $i<count($rows); $i++) {
                                    $time = $rows[$i]["time"];
                                    $sf = $rows[$i]["sf"];
                                    $city = $rows[$i]["city"];
                                    $xian = $rows[$i]["xian"];
                                    $dizhi = $rows[$i]["dizhi"];
                                    $beizhu = $rows[$i]["beizhu"];
                                    $address = $sf . $city . $xian . $dizhi;
                                    $timestamp = $time;
                                    $date = date('Y-m-d', $timestamp);
                                ?>
                                <div class="row">
                                    <span class="col"><?php echo $date ?></span>
                                    <span class="col"><?php echo $address ?></span>
                                     <span class="col"><?php echo strlen($beizhu) > 30 ? substr($beizhu, 0, 30) . '...' : $beizhu ?></span>
                                    <span class="icon-dot"></span>
                                </div>
                                <?php
                                }
                                ?>
                            </div>
                        </div>
                    </div>
                    <!--<div class="content">-->
                    <!--    <div class="head">-->
                    <!--        <span class="col">异常时间</span>-->
                    <!--        <span class="col">异常IP地址</span>-->
                    <!--        <span class="col">异常原因</span>-->
                    <!--    </div>-->
                    <!--    <div class="marquee-view">-->
                    <!--        <div class="marquee">-->
                    <!--            <div class="row">-->
                    <!--                <span class="col">20190701</span>-->
                    <!--                <span class="col">北京市昌平区建材城西路金燕龙写字楼</span>-->
                    <!--                <span class="col">1000001</span>-->
                    <!--                <span class="icon-dot"></span>-->
                    <!--            </div>-->
                    <!--        </div>-->
                    <!--    </div>-->
                    <!--</div>-->
                </div>
            </div>

            <!--点位-->
            <div class="point panel">
                <div class="inner">
                    <h3>代理地区分布统计</h3>
                    <div class="chart">
                        <div class="pie"></div>
                        <div class="data">
                            <div class="item">
                                <h4><?php echo $proxyCount ?></h4>
                                <span>
                                    <i class="icon-dot" style="color: #ed3f35"></i>
                                    代理总数
                                </span>
                            </div>
                            <div class="item">
                                <h4><?php echo $newProxyCount ?></h4>
                                <span>
                                    <i class="icon-dot" style="color: #eacf19"></i>
                                    本月新增
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="column">
            <!-- 地图 -->
            <div class="map">
                <h3>
                    <span class="icon-cube"></span>
                    用户分布数据统计
                </h3>
                <div class="chart">
                    <div class="geo"></div>
                </div>
            </div>
            <!-- 用户 -->
            <?php
            include_once("../../untils/conn.php");
            mysqli_query($con, "set names utf8");
            $cities = array();
            $result = mysqli_query($con, "SELECT DISTINCT city FROM oder");
            while ($row = mysqli_fetch_assoc($result)) {
                array_push($cities, $row["city"]);
            }
            
            $orderCounts = array();
            $sql = "SELECT COUNT(*) as count FROM oder GROUP BY city";
            $result = mysqli_query($con, $sql);
            while ($row = mysqli_fetch_assoc($result)) {
                array_push($orderCounts, $row["count"]);
            }
            
            ?>
                        
            <div class="users panel">
                <div class="inner">
                    <h3>全国客户总量统计</h3>
                    <div class="chart">
                        <div class="bar"></div>
                        <div class="data">
                            <div class="item">
                                <h4><?php echo $uniqueUidCount ?></h4>
                                <span>
                                    <i class="icon-dot" style="color: #ed3f35"></i>
                                    用户总量
                                </span>
                            </div>
                            <div class="item">
                                <h4><?php echo $uniqueUidCountThisMonth ?></h4>
                                <span>
                                    <i class="icon-dot" style="color: #eacf19"></i>
                                    本月新增
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="column">
            <!-- 订单 -->
         <?php
            include_once("../../untils/conn.php");
            mysqli_query($con, "set names utf8");
            
            session_start();
            
            // 获取上次请求时的订单数量和总价
            $num_today_order_last = isset($_SESSION['num_today_order']) ? $_SESSION['num_today_order']['num'] : 0;
            $total_price_today_order_last = isset($_SESSION['num_today_order']) ? $_SESSION['num_today_order']['total_price'] : 0;
            
            // 获取今天的日期
            $today = date('Y-m-d');
            
            // 查询今天的订单数量和总价
            $sql = "SELECT COUNT(*) as num, SUM(price) as total_price FROM oder WHERE DATE(FROM_UNIXTIME(`time`))='{$today}'";
            $result = mysqli_query($con, $sql);
            
            if ($result === false) {
                echo "SQL执行失败，错误信息为：" . mysqli_error($con);
            } else {
                $row = mysqli_fetch_assoc($result);
                $num_today_order = $row['num'];
                $total_price_today_order = $row['total_price'];
            
                // 比较两次订单数量，如果数量增加了1，则播放音乐
                if ($num_today_order_last < $num_today_order && $num_today_order - $num_today_order_last === 1) {
                    // 播放音乐的逻辑
                    echo '<audio src="https://cdn.qiniu.junes.cn/upload/2023/06/news.mp3" autoplay></audio>';
                }
            
                // 将当前订单数量和总价保存到 session
                $_SESSION['num_today_order'] = array(
                    'num' => $num_today_order,
                    'total_price' => $total_price_today_order
                );
            }
            

            ?>
            
            <div class="order panel">
                <div class="inner">
                    <div class="filter">
                        <a href="#">今日实时订单佣金数据记录</a>
                    </div>
                    <!-- 数据 -->
                    <div class="data">
                        <div class="item">
                            <h4><?php echo $num_today_order ?></h4>
                            <span>
                                <i class="icon-dot" style="color: #ed3f35;"></i>
                                今日订单
                            </span>
                        </div>
                        <div class="item">
                            <h4><?php echo isset($total_price_today_order) ? $total_price_today_order : 0; ?></h4>
                            <span>
                                <i class="icon-dot" style="color: #eacf19;"></i>
                                今日预估佣金(元)
                            </span>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 销售额 -->
            <div class="sales panel">
                <div class="inner">
                    <div class="caption">
                        <h3>佣金统计</h3>
                        <a href="javascript:;" class="active" data-type="year">年</a>
                        <a href="javascript:;" data-type="quarter">季</a>
                        <a href="javascript:;" data-type="month">月</a>
                        <a href="javascript:;" data-type="week">周</a>
                    </div>
                    <div class="chart">
                        <div class="label">单位:元</div>
                        <div class="line"></div>
                    </div>
                </div>
            </div>
            <!-- 渠道 季度 -->
            <?php
            include_once("../../untils/conn.php");
            mysqli_query($con, "set names utf8");
            
            // 查询渠道表
            $sql = "SELECT * FROM channel";
            $result = mysqli_query($con, $sql);
            
            // 计算三个月前的时间戳
            $date = date('Y-m-d H:i:s', strtotime('-3 months'));
            $timestamp = strtotime($date);
            
            // 初始化订单总数变量
            $total_orders = 0;
            
            // 循环遍历渠道信息
            while ($channel = mysqli_fetch_array($result)) {
              $channel_id = $channel['channel_id'];
              $channel_name = $channel['channel_name'];
            
              // 查询该渠道三个月内的订单数量
              $sql = "SELECT COUNT(*) as num FROM oder WHERE `time` >= {$timestamp} AND `channel` = {$channel_id}";
              $result2 = mysqli_query($con, $sql);
              $orders = mysqli_fetch_array($result2)['num'];
            
              // 累加订单数量到总数
              $total_orders += $orders;
            }
            
            // 初始化渠道占比信息数组
            $channel_ratios = array();
            
            // 循环遍历渠道信息，计算每个渠道订单数量占比，并保存到数组中
            mysqli_data_seek($result, 0); // 将查询结果句柄指针重置
            while ($channel = mysqli_fetch_array($result)) {
              $channel_id = $channel['channel_id'];
              $channel_name = $channel['channel_name'];
            
              // 查询该渠道三个月内的订单数量
              $sql = "SELECT COUNT(*) as num FROM oder WHERE `time` >= {$timestamp} AND `channel` = {$channel_id}";
              $result2 = mysqli_query($con, $sql);
              $orders = mysqli_fetch_array($result2)['num'];
            
              // 计算该渠道订单数量占比
              $ratio = ($total_orders > 0) ? round($orders/$total_orders*100) : 0;
            
              // 将占比信息保存到数组中
              $channel_ratios[] = array('name'=>$channel_name, 'ratio'=>$ratio);
            }
            ?>
            <div class="wrap">
                <div class="channel panel">
                    <div class="inner">
                        <h3>渠道分布</h3>
                       <?php for ($i = 0; $i < count($channel_ratios); $i += 2) {  ?>
                        <div class="data">
                            <div class="item">
                                <h4><?php echo $channel_ratios[$i]['ratio'] ?> <small>%</small></h4>
                                <span>
                                    <i class="icon-dot"></i>
                                  <?php echo $channel_ratios[$i]['name']?>
                                </span>
                            </div>
                            
                            <?php  if (isset($channel_ratios[$i+1])) { ?>
                            <div class="item">
                                <h4><?php echo $channel_ratios[$i+1]['ratio'] ?> <small>%</small></h4>
                                <span>
                                    <i class="icon-dot"></i>
                                  <?php echo $channel_ratios[$i+1]['name'] ?>
                                </span>
                            </div>
                            <?php } ?>
                        </div>
                        <?php } ?>
                    </div>
                </div>
                
                
                
                <?php 
                // 三个月前的日期
                include_once("../../untils/conn.php");
                mysqli_query($con, "set names utf8");
                $date = date('Y-m-d', strtotime('-3 month'));
                
                $sql = "SELECT COUNT(*) as jidu FROM oder WHERE `time` >= UNIX_TIMESTAMP('{$date}')";
                
                $result = mysqli_query($con, $sql);
                
                if ($result === false) {
                  die(mysqli_error($con)); // 如果查询出错则输出错误信息
                }
                
                $row = mysqli_fetch_assoc($result);
                $jidu = $row['jidu'];
                ?>
                <div class="quarter panel">
                    <div class="inner">
                        <h3>季度佣金进度</h3>
                        <div class="chart">
                            <div class="box">
                                <div class="gauge"></div>
                                <div class="label">70<small> %</small></div>
                            </div>
                            <div class="data">
                                <div class="item">
                                    <h4><?php echo $jidu *110  ?></h4>
                                    <span>
                                        <i class="icon-dot" style="color: #6acca3"></i>
                                        佣金(元)
                                    </span>
                                </div>
                                <div class="item">
                                    <h4>150%</h4>
                                    <span>
                                        <i class="icon-dot" style="color: #ed3f35"></i>
                                        同比增长
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- 排行榜 -->
             <?php
            include_once("../../untils/conn.php");
            mysqli_query($con, "set names utf8");
            
            $sql = "SELECT goods, COUNT(*) AS count FROM oder GROUP BY goods ORDER BY count DESC LIMIT 3";
            
            $result = $con->query($sql);
            
            $rows = array();
            
            if ($result->num_rows > 0) {
                while($row = $result->fetch_assoc()) {
                    $rows[] = $row;
                }
            } else {
                echo "未找到匹配数据";
            }
            ?>
            <div class="top panel">
                <div class="inner">
                    <div class="all">
                        <h3>全国热榜</h3>
                        <ul>
                            <?php
                            $icons = array('icon-cup1', 'icon-cup2', 'icon-cup3');
                            for($i=0; $i<count($rows); $i++) {
                                $row = $rows[$i];
                                echo '<li class="item-' . ($i+1) . '"><i class="' . $icons[$i] . '" style="color: #d93f36;"></i>';
                                echo $row['goods']; // 输出商品名
                                echo '</li>';
                            }
                            ?>
                        </ul>
                    </div>
                    
                    <?php
                        // 连接数据库
                        include_once("../../untils/conn.php");
                        mysqli_query($con, "set names utf8");
                    
                        // 查询sf列中出现最多城市及销量
                        $sql = "SELECT sf, COUNT(*) as count FROM oder GROUP BY sf ORDER BY COUNT(*) DESC";
                        $result = mysqli_query($con, $sql);
                    
                        // 检查查询结果是否存在错误
                        if (!$result) {
                            printf("Error: %s\n", mysqli_error($con));
                            exit();
                        }
                    ?>
                    
                    <div class="province">
                        <h3>各省热销 <i class="date">// 近30日 //</i></h3>
                        <div class="data">
                            <ul class="sup">
                                <?php
                                    while ($row = mysqli_fetch_assoc($result)) {
                                ?> 
                                <li>
                                    <span><?php echo $row['sf']; ?></span>
                                    <span><?php echo number_format($row['count']); ?> <s class="icon-up"></s></span>
                                </li>    
                                <?php
                                    }
                                ?>
                            </ul>
                            <ul class="sub">
                                <!-- <li><span>数据</span><span> 数据<s class="icon-up"></s></span></li> -->
                            </ul>
                        </div>
                    </div>
                    
                </div>
            </div>
        </div>
    </div>
</body>
<script type="text/javascript">
    var cityData = <?php echo json_encode($cities); ?>;
    var orderData = <?php echo json_encode($orderCounts); ?>;
    // 在控制台打印城市信息
    console.log(cityData);
    console.log(orderData);
</script>

<script type="text/javascript">
    var timeLeft = 10;
    var countdown = setInterval(function() {
        timeLeft--;
        document.getElementById("countdown").textContent = timeLeft;
        if (timeLeft <= 0) {
            clearInterval(countdown);
            window.location.reload();
        }
    }, 1000);
</script>

<script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script>
<script src="https://www.jq22.com/jquery/echarts-4.2.1.min.js"></script>
<script src="./js/index.js"></script>

<script src="./js/china.js"></script>
<script src="./js/mymap.js"></script>

</html>

<?php
} else {
  echo "<script>alert('您尚未登录，没有权限访问该页面');location.href='../login.php';</script>";
}